<template>
  <div class="record mx-5 my-5">
    <div>
      <el-table
        :data="list"
        :empty-text="loading.list ? '数据加载中' : '暂无数据'"
        cell-class-name="custom-row"
        header-row-class-name="custom-row"
        header-cell-class-name="custom-header"
      >
        <el-table-column label="饰品">
          <template #default="scope">
            {{ scope.row.item_name }}
          </template>
        </el-table-column>
        <el-table-column label="价格">
          <template #default="scope"> {{ scope.row.price }} T币 </template>
        </el-table-column>
        <el-table-column label="售卖时间">
          <template #default="scope">
            {{
              $dayjs.unix(scope.row.created_at).format("YYYY-MM-DD HH:mm:ss")
            }}
          </template>
        </el-table-column>
      </el-table>

      <div class="d-flex my-5 justify-center">
        <el-pagination
          :page-size="10"
          :total="total"
          background
          hide-on-single-page
          layout="prev, pager, next"
          @current-change="currentChange"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, reactive, toRefs } from "vue";
import { BackpackService } from "@/services/BackpackService";
import Empty from "@/components/empty/Index.vue";

export default defineComponent({
  components: { Empty },
  setup() {
    const state = reactive({
      list: [],
      total: null,
      loading: {
        list: false,
      },
    });

    const sellRecord = async () => {
      state.loading.list = true;
      const res = await BackpackService.sellRecord().finally(() => {
        state.loading.list = false;
      });
      state.list = res.data.data.list;
      state.total = res.data.data.total;
    };

    const currentChange = async (val) => {
      state.currentPage = val;
      const res = await BackpackService.sellRecord({
        page: val,
      });

      state.list = res.data.data.list;
    };

    onMounted(() => {
      sellRecord();
    });

    return {
      ...toRefs(state),
      currentChange,
    };
  },
});
</script>
